package com.tenebrous.jestic.controller.framwork.meber.author;

import com.tenebrous.jestic.compents.annotations.Weblog;
import com.tenebrous.jestic.compents.annotations.WeblogEnum;
import com.tenebrous.jestic.controller.SupperController;
import com.tenebrous.jestic.entity.framwork.meber.author.AuthRouter;
import com.tenebrous.jestic.service.framwork.meber.author.AuthRouterService;
import com.rain.ops.sdk.dto.ApiResult;
import com.rain.ops.sdk.utils.UUIDUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Date;

@Validated
@RestController
@RequestMapping("/author/router")
public class RouterController extends SupperController {

    @Autowired
    private AuthRouterService routerService;

    @PostMapping("/get")
    public ApiResult get(@NotBlank String id) {
        AuthRouter role = routerService.selectByPrimaryKey(id);
        if(role != null) {
            return new ApiResult(200, "查询成功。", role);
        }
        return new ApiResult(999, "查询失败，未找到指定对象。");
    }

    /**
     * 分页查询
     * @param name
     * @param path
     * @return
     */
    @PostMapping("/paging")
    public ApiResult paging( String name, String path) {
        return new ApiResult(200, "查询成功。", routerService.queryPagingList(super.getPageIndex(), super.getPageSize(), name, path));
    }

    /**
     * 更新对象
     * @param item
     * @return
     */
    @PostMapping("/update")
    @Weblog(description = "更新路由信息", method = WeblogEnum.UPDATE)
    public ApiResult update(@NotNull @Validated AuthRouter item) {
        if(StringUtils.isNotBlank(item.getId())) {
            item.setUpdateDate(new Date());
            routerService.updateSelective(item);
        } else {
            item.setId(UUIDUtils.randomUUID());
            item.setCreateDate(new Date());
            routerService.saveSelective(item);
        }
        return new ApiResult(200, "更新成功。");
    }

    /**
     * 删除指定路由信息
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    @Weblog(description = "删除指定路由信息", method = WeblogEnum.DELETE)
    public ApiResult delete(@NotEmpty String[] ids) {
        return new ApiResult(200, "删除成功。", routerService.deleteByIds(ids));
    }

}
